﻿using Arction.Wpf.Charting;
using HighThroughput.Common;
using HighThroughput.ShareData;
using MathNet.Numerics.LinearAlgebra.Double;
using OSSUtility;
using Ozone.BLL;
using Ozone.Common;
using Ozone.Device;
using Ozone.Help;
using Ozone.Models;
using Ozone.ShareData;
using Ozone.Views.Video;
using Ozone.Views.Widget;
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using static HighThroughput.ShareData.ReadData;

namespace HighThroughput
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        private List<Grid> lstTabGridContent = new List<Grid>();
        private List<Grid> lstTabButtonGrid = new List<Grid>();

        public MainWindow()
        {
            InitData();
            InitializeComponent();
            Init();
        }

        /// <summary>
        /// 初始化数据
        /// </summary>
        private void InitData()
        {
            //参数中心
            ParamCenter.g_ParamCenter = new ParamCenter();

            ParamCenter.Entity.Init();

            //数据中心
            ReadData.g_RealData = new ReadData();

            //日志
            AppLogData.g_AppLogData = new AppLogData();

            //设备中心
            DeviceCenter.g_DeviceCenter = new DeviceCenter();
        }

        private void InitView()
        {
            //TAB页对应的内容
            lstTabGridContent.Add(gridAppCenter);
            lstTabGridContent.Add(gridMethodCenter);
            lstTabGridContent.Add(gridSurveillanceCenter);
            lstTabGridContent.Add(gridSystemCenter);
            lstTabGridContent.Add(gridServiceCenter);
            lstTabGridContent.Add(gridUserCenter);

            //TAB页按钮
            lstTabButtonGrid.Add(gridTab1);
            lstTabButtonGrid.Add(gridTab2);
            lstTabButtonGrid.Add(gridTab3);
            lstTabButtonGrid.Add(gridTab4);
            lstTabButtonGrid.Add(gridTab5);
            lstTabButtonGrid.Add(gridTab6);
        }

        private void Init()
        {
            InitView();
            //清除用户凭据
            Tools.ClearCredentials();
        }

        /// <summary>
        /// 退出
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnExitClick(object sender, RoutedEventArgs e)
        {
            WindowConfirmationDialog windowConfirmationDialog = new WindowConfirmationDialog("确认要退出系统吗?");
            if (windowConfirmationDialog.ShowDialog() == false)
            {
                return;
            }
            Close();
        }

        /// <summary>
        /// 最小化
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnMin_Click(object sender, RoutedEventArgs e)
        {
            this.WindowState = WindowState.Minimized;
        }

        /// <summary>
        /// 页面加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void onLoad(object sender, RoutedEventArgs e)
        {
        }

        /// <summary>
        /// 鼠标左键点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MenuText_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            string tag = ((Grid)sender).Tag.ToString();
            int nIndex = Utility.str2int(tag);
            if (AccessControl(nIndex))  //权限管控
            {
                ChangeTabItem(nIndex);
            }
        }

        private void ChangeTabItem(int nIndex)
        {
            //找到资源 背景色
            var myResourceDictionaryShared = new ResourceDictionary
            {
                Source = new Uri("Resources/Styles/Shared.xaml", UriKind.RelativeOrAbsolute) // 指定样式文件的路径
                                                                                             //    Source = new Uri("Resources/Styles/Expander.xaml", UriKind.RelativeOrAbsolute) // 指定样式文件的路径
            };
            //按钮点击背景色
            SolidColorBrush Brush_PurpleBackColor = myResourceDictionaryShared["Brush_PurpleBackColor"] as SolidColorBrush; // 通过key找到指定的样式
            //按钮背景色
            SolidColorBrush Brush_MainBackColor = myResourceDictionaryShared["Brush_DarkBackColor3"] as SolidColorBrush; // 通过key找到指定的样式
            for (int i = 0; i < lstTabGridContent.Count; i++)
            {
                if (i == nIndex)
                {
                    lstTabButtonGrid[i].Background = Brush_PurpleBackColor;
                    lstTabGridContent[i].Visibility = Visibility.Visible;
                }
                else
                {
                    lstTabButtonGrid[i].Background = Brush_MainBackColor;
                    lstTabGridContent[i].Visibility = Visibility.Collapsed;
                }
            }
        }

        /// <summary>
        /// 用户登录按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnLoginClick(object sender, RoutedEventArgs e)
        {
            WindowLogin windowLogin = new WindowLogin();
            if (windowLogin.ShowDialog() == true)
            {
                loginUser.UserName = globalVar.currentSys_UserModel.Username;
            }
            else
            {
                loginUser.UserName = "未登录";
            }
        }

        /// <summary>
        /// 权限管控
        /// 业务员      1   只能看  和不登录权限一样
        /// 操作员      0    流程中心  方法中心  数据中心  服务中心
        /// 管理员      2   流程中心  方法中心  数据中心  系统中心  服务中心
        /// 超级管理员  3   流程中心  方法中心  数据中心  系统中心  服务中心  用户中心  不进行限制
        /// </summary>
        public bool AccessControl(int tag)
        {
            if (tag > 0)  //除了流程中心
            {
                if (globalVar.currentSys_UserModel == null)
                {
                    return PromptInfo(InfoEnum.DialogType.Warning.ToString(), "账号未登录,请先登录账号！");
                }
                else if (globalVar.currentSys_UserModel.RoleId == "1")   //业务员
                {
                    return PromptInfo(InfoEnum.DialogType.Warning.ToString(), "当前账号暂无查看权限！");
                }
                else if (globalVar.currentSys_UserModel.RoleId == "0")  //操作员
                {
                    if (tag == 3 || tag == 5)
                    {
                        return PromptInfo(InfoEnum.DialogType.Warning.ToString(), "当前账号暂无查看权限！"); ;
                    }
                }
                else if (globalVar.currentSys_UserModel.RoleId == "2")
                {
                    if (tag == 5)
                    {
                        return PromptInfo(InfoEnum.DialogType.Warning.ToString(), "当前账号暂无查看权限！");
                    }
                }
            }
            return true;
        }

        public bool PromptInfo(string dialogType, string promptInfo)
        {
            Tools.ShowPromptDialog(dialogType, "账号未登录,请先登录账号！");
            ChangeTabItem(0);
            return false;
        }
    }
}